package com.thor.palmmall.impl.statement.job;

import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.thor.commons.query2.QueryDefinition2;
import com.thor.palmmall.api.statement.AStatementPayBill;
import com.thor.palmmall.api.statement.AStatementPayBillState;
import com.thor.palmmall.api.statement.StatementService;
import com.thor.palmmall.rest.event.StatementOrderSyncEvent;
import com.thor.palmmall.util.event.GlobalEventBus;
import com.thor.util.ApplicationContextUtils;

/**
 * @author liyang
 *
 * @since 1.0
 */
@DisallowConcurrentExecution
public class StatementPayBillJob extends QuartzJobBean {

  @Override
  protected void executeInternal(JobExecutionContext paramJobExecutionContext)
      throws JobExecutionException {
    QueryDefinition2 definition = new QueryDefinition2();
    definition.addCondition(StatementService.CONDITION_STATE_IN, AStatementPayBillState.PAID);
    definition.addCondition(StatementService.CONDITION_NOTIFYFLAG_EQUALS, "success");
    definition.addCondition(StatementService.IS_FILTTER_TRY_MAX, false);
    definition.setPageNumber(0);
    definition.setPageSize(100);
    List<AStatementPayBill> list = getStatementService().query(definition).getRecords();
    for (AStatementPayBill bill : list) {
      StatementOrderSyncEvent event = new StatementOrderSyncEvent();
      event.setBill(bill);
      GlobalEventBus.post(event);
    }
  }

  private StatementService getStatementService() {
    return ApplicationContextUtils.getBean(StatementService.class);
  }
}
